<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\timbre;



use app\common\lib\MoyinTts;
use app\common\lib\TencentCos;
use app\common\lib\TencentTts;
use plugin\admin\app\common\Tree;
use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\moyin\TimbreModel;
use support\Request;
use support\Response;

class TimbreLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new TimbreModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','sort');
            $request->inputSet('order','desc');
        }
        if($request->input('spread_type')==3){
            $request->inputSet('lang',['<>','普通话']);
        }elseif($request->input('spread_type')==1){
            $request->inputSet('lang','普通话');
        }

        $request->inputSet('isdel','0');
        return parent::select($request);
    }

    /**
     * 格式化表格树
     * @param $items
     * @return Response
     */
    protected function formatTree($items): Response
    {
        $pData = [];
        foreach($items as $item){
            $pData[$item['trait']] = [
                'id'=>$item['trait'],
                'name'=>$item['trait'],
                'pid'=>'0',
                'value'=>$item['trait'],
            ];
        }

        $data = [];
        foreach($items as $item){
            if(!isset($pData[$item['trait']])){
                continue;
            }
            $pDataMap = $pData[$item['trait']];
            $data[] = [
                'name'=>$item['name'].'('.$item['scenes'].')('.$item['lang'].')<a href="javascript:listen(\''.$item['example_url'].'\');">试听</a>',
                'value'=>$item['id'],
                'id'=>$item['id'],
                'pid'=>$pDataMap['id'],
            ];
        }

        $data = array_merge(array_values($pData),$data);
        $tree = new Tree($data);
        return json(['code' => 0, 'msg' => 'ok', 'data' => $tree->getTree()]);
    }

    public function insert(Request $request): Response
    {

        return parent::insert($request);
    }

    protected function doInsert(array $data)
    {
        if(!empty($data['timbreid'])){
            $timbreExampleText = '';
            if($data['lang']=='普通话'){
                $timbreExampleText = config('param.timbre_example_text');
            }elseif($data['lang']=='英文'){
                $timbreExampleText = config('param.timbre_example_text_en');
            }elseif($data['lang']=='泰语'){
                $timbreExampleText = config('param.timbre_example_text_th');
            }elseif($data['lang']=='印尼语'){
                $timbreExampleText = config('param.timbre_example_text_id');
            }

            $result = timbreTextToVoice($timbreExampleText,['speaker'=>$data['timbreid']]);
            $data['example_url'] = $result['voice_url'];
        }
        return parent::doInsert($data);
    }

    public function update(Request $request): Response
    {
        return parent::update($request);
    }

    protected function doUpdate($id, $data)
    {
        $first = $this->model->find($id);
        if(!empty($data['lang']) && (empty($first['example_url']) || (!empty($data['timbreid']) && $first['timbreid']!=$data['timbreid']))){
            $timbreExampleText = '';
            if($data['lang']=='普通话'){
                $timbreExampleText = config('param.timbre_example_text');
            }elseif($data['lang']=='英文'){
                $timbreExampleText = config('param.timbre_example_text_en');
            }elseif($data['lang']=='泰语'){
                $timbreExampleText = config('param.timbre_example_text_th');
            }elseif($data['lang']=='印尼语'){
                $timbreExampleText = config('param.timbre_example_text_id');
            }

            $result = timbreTextToVoice($timbreExampleText,['speaker'=>$data['timbreid']]);
            $data['example_url'] = $result['voice_url'];
        }
        return parent::doUpdate($id, $data);
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }

    public function trait(Request $request): Response
    {
        $trait = $this->model->getWhere(['isdel'=>0])->groupBy('trait')->pluck('trait')->toArray();
        $formatted_items = [];
        foreach ($trait as $item) {
            $formatted_items[] = [
                'name' => $item,
                'value' => $item
            ];
        }
        return  json(['code' => 0, 'msg' => 'ok', 'data' => $formatted_items]);
    }
}